from application import app, db
from flask import Blueprint, request, jsonify, make_response, redirect
from common.libs.Helper import ops_renderJSON, ops_renderErrJSON, ops_render
from common.libs.DataHelper import getCurrentTime
from common.libs.UrlManager import UrlManager
from common.libs.UserService import UserService
from common.models.user import User

member_page = Blueprint("member_page", __name__)


@member_page.route("/reg", methods=["GET", "POST"])
def reg():
    if request.method == "GET":
        return ops_render("member/reg.html")
    req = request.values
    login_name = req["login_name"] if "login_name" in req else ""
    login_pwd = req["login_pwd"] if "login_pwd" in req else ""
    login_qrpwd = req["login_qrpwd"] if "login_qrpwd" in req else ""
    if login_name is None or len(login_name) < 1:
        return ops_renderErrJSON("请输入正确的用户名-2")
    else:
        if login_pwd is None or len(login_pwd) < 6:
            return ops_renderErrJSON("请输入合法的密码-2")
        elif login_pwd != login_qrpwd:
            return ops_renderErrJSON("两次密码不正确-2")
        else:
            user_info = User.query.filter_by(login_name=login_name).first()
            if user_info:
                return ops_renderErrJSON("登录用户名已被注册")
            else:
                model_user = User()
                model_user.login_name = login_name
                model_user.nickname = login_name
                model_user.login_salt = UserService.geneSalt(8)
                model_user.login_pwd = UserService.genePwd(login_pwd, model_user.login_salt)
                model_user.created_time = model_user.updated_time = getCurrentTime()
                db.session.add(model_user)
                db.session.commit()

                return ops_renderJSON(msg="注册成功")


@member_page.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "GET":
        return ops_render("member/login.html")
    req = request.values
    login_name = req["login_name"] if "login_name" in req else ""
    login_pwd = req["login_pwd"] if "login_pwd" in req else ""
    if login_name is None or len(login_name) < 1:
        return ops_renderErrJSON("用户名不存在-2")
    elif login_pwd is None or len(login_pwd) < 6:
        return ops_renderErrJSON("密码输入错误-2")
    else:
        user_info = User.query.filter_by(login_name=login_name).first()
        if not user_info:
            return ops_renderErrJSON("用户名不存在-3")
        elif user_info.login_pwd != UserService.genePwd(login_pwd, user_info.login_salt):
            return ops_renderErrJSON("密码输入错误-3")
        elif user_info.status != 1:
            return ops_renderErrJSON("账号被禁用，请联系管理员")
        else:
            # session["uid"]=user_info.id
            response = make_response(ops_renderJSON(msg="登录成功，正在跳转"))
            # "#"验证位
            response.set_cookie(app.config["AUTH_COOKIE_NAME"],
                                "%s#%s" % (UserService.geneAuthCode(user_info), user_info.id), 60 * 60 * 24 * 120)
            return response


@member_page.route("/logout")
def logOut():
    response = make_response(redirect(UrlManager.buildUrl("/")))
    response.delete_cookie(app.config["AUTH_COOKIE_NAME"])
    return response
